home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Starter / Source / MultipleScreenManager.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  240 lines

  1.  
  2. /*     MultipleScreenManager.m
  3.     created by Robert Vasvari - July 1994
  4.  
  5.     This class is an abstract superclass that is
  6.     supposed to make it easier to add or modify
  7.     multi screen preferences panels.
  8.     The steps to take to add a new screen:
  9.     1: make a new box in IB that contains the matrices,
  10.        textfields etc, and fit it into the mid-section
  11.        of the panel. At runtime the subviews of the panel
  12.        will be parsed. All boxes that have a title in the form:
  13.        "screen box %d" will be hooked up to the popup cell with
  14.        tag %d.
  15.     2: set the necessary pointers to the box and its contents.
  16.     4: edit the following methods:
  17.         - setupBoxes;
  18.         - loadDataFor:(int)boxTag;
  19.         - dataChanged:sender;
  20.         - set:sender;
  21.     Hopefully it will just work...
  22. */
  23.  
  24. #import "MultipleScreenManager.h"
  25. #import "Controller.h"
  26. #import "util.h"
  27.  
  28. @implementation MultipleScreenManager
  29.  
  30. - init
  31. {    [super init];
  32.     currentBoxTag=0;
  33.     needsDisplay=YES;
  34.     return self;
  35. }
  36.  
  37. - awakeFromNib
  38. {
  39. int t, i=0, count=0;
  40. id view=nil, cell=nil, matrix=[[mainPopup target] itemList];
  41. id list;
  42. char *p;
  43.  
  44.     maxIndex= -1;
  45.     list=[[panel contentView] subviews];
  46.     count=[list count];
  47.     if(!list) return self;
  48.     
  49.     for(i=0;i<count;i++)
  50.      { p=NULL;
  51.        t=0;
  52.        view=[list objectAt:i];
  53.        if([view isKindOf:[Box class]])
  54.         { if(!(p=(char *)[view title])) continue;
  55.           if(strncmp(p,"screen box ",11)) continue;
  56.           sscanf(p,"screen box %d",&t);
  57.           if(!(cell=[matrix findCellWithTag:t]))
  58.            { printf(
  59.               "bad boxtag in MultipleScreenManager setupBoxes:%d\n",t); 
  60.              continue;
  61.            }
  62.           boxes[t]=view; 
  63.           [view setTitlePosition:NX_NOTITLE];
  64.           if(t>maxIndex) maxIndex=t;
  65.           t++;
  66.         }
  67.      }
  68.  
  69.     i=0;
  70.     while(boxes[i])
  71.      { [boxes[i++] removeFromSuperview];
  72.      }
  73.     return self;
  74. }
  75.  
  76. - setNeedsDisplay:(BOOL)flag
  77. {    needsDisplay=flag;
  78.     return self;
  79. }
  80.  
  81. - (BOOL)needsDisplay
  82. {    return needsDisplay;
  83. }
  84.  
  85. - (int)currentBoxTag
  86. {    return currentBoxTag;
  87. }
  88.  
  89. - loadDataFor:(int)boxTag
  90. {
  91.     /* general purpose method to display a box specified by the tag
  92.            in the preferences panel 
  93.       */
  94.     /* here should be a switch statement to load in all
  95.        data for a particular screen 
  96.      */
  97.     return self;
  98. }
  99.  
  100. - dataChanged:sender
  101. {
  102.     /* this method figures out if there is any change in the currently
  103.        selected box and enables/disables the set/revert buttons */
  104.     
  105.     /* here should be a switch statement to figure out 
  106.        for a particular screen if there was a change
  107.      */
  108.     return self;
  109. }
  110.  
  111. - textDidGetKeys:textObj isEmpty:(BOOL)flag
  112. {    [self dataChanged:self];
  113.     return self;
  114. }
  115.  
  116. - set:sender
  117. {    
  118.     /* here should be a switch statement to
  119.        save all the changes
  120.      */
  121.     return self;
  122. }
  123.  
  124. - changeBox:sender
  125. {
  126.     if([panel isDocEdited])
  127.      { switch(NXRunAlertPanel("Alert",
  128.         "Recent changes that haven't been saved will be lost!",
  129.         "Save Changes","Close Anyway","Cancel"))
  130.           { case NX_ALERTDEFAULT: [self set:setButton]; break;
  131.             case NX_ALERTALTERNATE: break;
  132.             case NX_ALERTOTHER: return nil;
  133.             default: return nil;
  134.           }
  135.      }
  136.     [panel disableFlushWindow];
  137.     [self getPanelWithScreen:[[sender selectedCell] tag]];
  138.     [panel reenableFlushWindow];
  139.     [panel flushWindow];
  140.     return self;
  141. }
  142.  
  143. - getPanelWithScreen:(int)index
  144. {
  145.     [[[mainPopup target] itemList] selectCellWithTag:index];
  146.     [mainPopup setTitle:
  147.      [[[[mainPopup target] itemList] selectedCell] title]];
  148.     [self displayBox:index];
  149.     [revertButton setEnabled:NO];
  150.     [setButton setEnabled:NO];
  151.     [panel setDocEdited:NO];
  152.     [panel display];
  153.     [panel makeKeyAndOrderFront:nil];
  154.     return self;
  155. }
  156.  
  157. - displayBox:(int)boxTag
  158. {
  159.     if(!boxes[boxTag])
  160.      { fprintf(stderr,"displayBox: bad boxTag:%d\n",boxTag);
  161.        return self;
  162.      }
  163.  
  164.     /* remove the old box */
  165.     if([boxes[currentBoxTag] superview])
  166.         [boxes[currentBoxTag] removeFromSuperview];
  167.  
  168.     currentBoxTag=boxTag;
  169.      
  170.     /* add the new box :
  171.        if the noSelectionBox variable is set, and there is no currentDoc,
  172.        use it
  173.      */ 
  174.     if(noSelectionBox)
  175.      {    if([[NXApp delegate] currentDoc])
  176.             {     if([noSelectionBox superview])
  177.                     [noSelectionBox removeFromSuperview];
  178.             if(![boxes[currentBoxTag] superview])
  179.                 [[panel contentView] addSubview:boxes[currentBoxTag]];
  180.             }
  181.         else
  182.             { if(![noSelectionBox superview])
  183.                     [[panel contentView] addSubview:noSelectionBox];
  184.             }
  185.      }
  186.     else
  187.      { if(![boxes[currentBoxTag] superview])
  188.                 [[panel contentView] addSubview:boxes[currentBoxTag]];
  189.      }
  190.     [self loadDataFor:boxTag];
  191.     return self;
  192. }
  193.  
  194. - revert:sender
  195. {    
  196.     [panel disableDisplay];
  197.     [panel disableFlushWindow];
  198.     [self loadDataFor:[[[[mainPopup target] itemList] selectedCell] tag]];
  199.      if([revertButton isEnabled]) [revertButton setEnabled:NO];
  200.     if([setButton isEnabled]) [setButton setEnabled:NO];
  201.     [panel setDocEdited:NO];
  202.     [panel reenableDisplay];
  203.     [panel display];
  204.     [panel reenableFlushWindow];
  205.     [panel flushWindow];
  206.     return self;
  207. }
  208.  
  209. - windowWillClose:sender 
  210. {    if([panel isDocEdited])
  211.      { switch(NXRunAlertPanel("Alert",
  212.         "Recent changes that haven't been saved will be lost!",
  213.         "Save Changes","Close Anyway","Cancel"))
  214.           { case NX_ALERTDEFAULT: [self set:setButton]; break;
  215.             case NX_ALERTALTERNATE: break;
  216.             case NX_ALERTOTHER: return nil;
  217.             default: return nil;
  218.           }
  219.      }
  220.     return self;
  221. }
  222.  
  223. - windowDidUpdate:sender
  224. {    
  225.     //printf("in Inspector windowDidUpdate\n");
  226.     if([panel isVisible]) [self displayBox:currentBoxTag];
  227.     return self;
  228. }
  229.  
  230. - free
  231. {    [[mainPopup target] free]; //a window
  232.     [panel free];
  233.     return [super free];
  234. }
  235.  
  236.  
  237.  
  238. @end
  239.  
  240.